<?php
/**
 * Created by PhpStorm.
 * User: DaibiDaddy
 * Date: 2016/6/3
 * Time: 14:59
 */

namespace App\Setting\LazyCommon;

use App\Model\LazyCommon\RulesCategoryModel;

class RulesSetting{

    /*手动获取的配置项*/
    public $entityCnName = "资讯";//模块名称
    public $table = "Rules";//数据表名称
    public $editKeys = array("k","v");//后台管理列表中可以直接编辑的字段
    public $perPage = 100;//后台管理列表中可以直接编辑的字段

    /*自动获取的配置项*/
    public $bundleName;
    public $entityName;
    public $SettingPath;
    public $entityPath;
    public $entity;
    public $modelPath;
    public $em;

    public function __construct($em)
    {
        $this->bundleName = "App";
        $this->entityName = str_replace("\\","",str_replace("Setting","",str_replace(__NAMESPACE__,"",__CLASS__)));
        $this->SettingPath = __CLASS__;
        $this->entityPath = str_replace($this->entityName."Entity",$this->entityName,str_replace("Setting","Entity",str_replace("\\LazyCommon","",__CLASS__)));
        $this->entity = new $this->entityPath();
        $this->modelPath = str_replace("Setting","Model",__CLASS__);
        $this->em = $em;
    }

    public function listConfig()
    {
        $default = array();
        $default["Rules"]["select"] = array("id","name","k","v","tips");
        $default["Rules"]["search"] = array("name","k","v","tips");
        $default["Rules"]["condition"] = array();
        $default["Rules"]["orderBy"] = array("id");

        $join = array();
        $join["Rules_Category AS RulesCategory"]["on"] = "Rules.c = RulesCategory.id";
        $join["Rules_Category AS RulesCategory"]["select"] = array("name");
        $join["Rules_Category AS RulesCategory"]["search"] = array();
        $join["Rules_Category AS RulesCategory"]["condition"] = array();
        $join["Rules_Category AS RulesCategory"]["orderBy"] = array();

        return array(
            "default"   => $default,
            "joins"     => $join,
        );
    }

    public function formConfig()
    {
        $model = new RulesCategoryModel($this->em);
        $parent = $model->getAllRulesCategoryTree();

        $parents[0] = "根目录";
        foreach ($parent as $k => $v) {
            $parents[$v["id"]] = $v["name"];
        }
        $options["category"]["other"] = array(
            "choices"   => array_flip($parents),
        );
        $config["c"] = array(
            "label"     =>"分组",
            "showType"  =>"select",
            "dataType"  =>"required",
            "option"    => $options["category"]
        );
        $config["name"] = array(
            "label"     =>"规则名称",
            "showType"  =>"text",
            "dataType"  =>"required",
            "option"    => array()
        );
        $config["k"] = array(
            "label"     =>"KEY",
            "showType"  =>"text",
            "dataType"  =>"required",
            "option"    => array()
        );
        $config["v"] = array(
            "label"     =>"VALUE",
            "showType"  =>"pic",
            "dataType"  =>"required",
            "option"    => array()
        );
        $config["tips"] = array(
            "label"     =>"说明",
            "showType"  =>"textarea",
            "dataType"  =>"required",
            "option"    => array()
        );
        return $config;
    }
}